/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.guiForFundamentalFilter;

import javax.swing.JDialog;
import presentation.mainJFrame;

/**
 *
 * @author TDINH
 */
public class CustomJPanel extends javax.swing.JPanel {

    /**
     * Creates new form CustomJPanel
     */
    public CustomJPanel() {
        initComponents();
        initOtherComponents();
    }

    public CustomTableModel getCustomTable() {
        return (CustomTableModel) customTablejTable.getModel();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        customTablejTable = new javax.swing.JTable();
        removeJButton = new javax.swing.JButton();
        addJButton = new javax.swing.JButton();

        customTablejTable.setModel(new CustomTableModel());
        jScrollPane1.setViewportView(customTablejTable);

        removeJButton.setText("Remove");
        removeJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeJButtonActionPerformed(evt);
            }
        });

        addJButton.setText("Add");
        addJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addJButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeJButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addJButton, removeJButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(removeJButton)
                    .addComponent(addJButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {addJButton, removeJButton});

    }// </editor-fold>//GEN-END:initComponents

    private void addJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJButtonActionPerformed
        addNewCriteriaJPanel.getParentDialog().setVisible(true);

        if (!addNewCriteriaJPanel.isOk()) {
            return;
        }

        CustomTableModel customTableModel = (CustomTableModel) this.customTablejTable.getModel();
        customTableModel.addRow(addNewCriteriaJPanel.getCriteria(), addNewCriteriaJPanel.getMin(), addNewCriteriaJPanel.getMax());
        customTablejTable.updateUI();
    }//GEN-LAST:event_addJButtonActionPerformed

    private void removeJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeJButtonActionPerformed
        int[] selectedRows = customTablejTable.getSelectedRows();

        CustomTableModel customTableModel = (CustomTableModel) this.customTablejTable.getModel();
        customTableModel.deleteRows(selectedRows);
        customTablejTable.updateUI();

    }//GEN-LAST:event_removeJButtonActionPerformed

    private void initOtherComponents() {
        newAddNewCriteriaJDialog();
    }
    
    public JDialog newAddNewCriteriaJDialog() {
        JDialog jDialog = new JDialog(mainJFrame.mainFrame, true);
        addNewCriteriaJPanel = new AddNewCriteriaJPanel(jDialog);
        jDialog.add(addNewCriteriaJPanel);
        jDialog.pack();
        return jDialog;
    }
    
    private AddNewCriteriaJPanel addNewCriteriaJPanel;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addJButton;
    private javax.swing.JTable customTablejTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeJButton;
    // End of variables declaration//GEN-END:variables
}
